﻿using AixCommInfo;
using ISaveReaddll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FinalTestMachine.View.AixCalcView
{
    /// <summary>
    /// UC_SimpleCalcMode.xaml 的交互逻辑
    /// </summary>
    public partial class UC_SimpleCalcMode : UserControl
    {
        public UC_SimpleCalcMode()
        {
            InitializeComponent();
            IUserLimitdll.PreControlBindingHelper.SetLitmitBinding(this, IUserLimitdll.UserLevelModes.ModifyAndOperation);
        }

        class MyCalculaor
        {



            /// <summary>
            /// 计算图卡透镜表面的距离
            /// </summary>
            /// <param name="arg1">模拟距离</param>
            /// <param name="arg2">工作距离</param>
            /// <returns></returns>
            public static string GetCharDis(string arg1, string arg2)
            {
                double x = 0;
                double y = 0;
                double z = 0;
                double t = 0;
                if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y))
                {
                    z = (((x - y - 2.74714) * 80) / (x - y - 2.74714 + 80)) + 6 - 1.13801;
                    //_charPos = z;
                    return z.ToString("F3");
                }
                throw new Exception("Input Err！");
            }

            /// <summary>
            /// 计算电机运动的位置
            /// </summary>

            /// <param name="arg2">起始点的位置</param>
            /// <returns></returns>
            public string GetMotionPos(string arg2)
            {

                double y = 0;
                double z = 0;
                if (double.TryParse(arg2, out y))
                {
                    // z = _charPos - y;
                    return z.ToString("F3");
                }
                return "参数输入有误，请确认！";
            }
            /// <summary>
            /// 计算电机运动的位置
            /// </summary>

            /// <returns></returns>
            public string GetMotionPos(string arg1, string arg2, string arg3)
            {
                double x = 0;
                double y = 0;
                double t = 0;
                double z = 0;
                if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y) && double.TryParse(arg3, out t))
                {
                    z = x - y;
                    return z.ToString("F3");
                }
                return "参数输入有误，请确认！";
            }
        }


        public class LightCalculaorParam
        {
            [SaveRemark]
            public ParamStructText<double> StartPos = new ParamStructText<double>();

            [SaveRemark]
            public ParamStructText<double> WorkPos = new ParamStructText<double>();

            [SaveRemark]
            /// <summary>
            /// 模拟距离
            /// </summary>
            public ParamStructText<double> SimulatePos = new ParamStructText<double>();

            [SaveRemark]
            /// <summary>
            /// 电机运行位置
            /// </summary>
            public ParamStructText<double> MotionrPos = new ParamStructText<double>();

            public event Action<double> CalcEvent;
            public void Calc()
            {
                double z = 0;
                double x = SimulatePos.GetValue;
                double y = WorkPos.GetValue;
                // z = (((x - y - 2.74714) * 80) / (x - y - 2.74714 + 80)) + 6 - 1.13801;
                z = 49.659 - StartPos.GetValue - (40*40/(x+34.4-y));
                // MotionrPos.GetValue = Math.Round((z - StartPos.GetValue), 3, MidpointRounding.AwayFromZero);
                MotionrPos.GetValue = Math.Round(z, 3, MidpointRounding.AwayFromZero);
                CalcEvent?.Invoke(MotionrPos.GetValue);

            }
        }
        LightCalculaorParam MyCalculaorParam__;
        public void SetBinding(LightCalculaorParam myCalculaorParam)
        {
            MyCalculaorParam__ = myCalculaorParam;
            void Set(ParamStructText<double> paramMsgText, TextBox textBox)
            {
                Binding binding = new Binding();
                binding.Source = paramMsgText;
                binding.Path = new PropertyPath("GetValue");
                binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
                textBox.SetBinding(TextBox.TextProperty, binding);
            }

            Set(myCalculaorParam.StartPos, tb_StartPos);
            Set(myCalculaorParam.WorkPos, tb_WorkPos);
            Set(myCalculaorParam.SimulatePos, tb_SimulatePos);
            Set(myCalculaorParam.MotionrPos, tb_MotionrPos);

        }
        private void Btn_Calc_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MyCalculaorParam__?.Calc();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
    }
}
